package com.haois.mp.wechat.handler;

import com.google.gson.Gson;
import com.haois.mp.database.commom.enums.MpTypeEnum;
import com.haois.mp.database.module.common.daomain.InviteRecord;
import com.haois.mp.database.module.common.daomain.UserToken;
import com.haois.mp.database.module.remote.service.IGroupService;
import com.haois.mp.database.module.offaccounts.service.RecordService;
import com.haois.mp.database.module.common.service.IUserTokenService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by RuiHaoZ on 2017/12/17 11:24
 */
@Component
public class ScanQRHandler extends AbstractHandler implements WxMpMessageHandler {
    @Autowired
    private IGroupService iGroupService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private IUserTokenService iUserTokenService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {

        String content = this.execute(wxMpXmlMessage,wxMpService);
        return WxMpXmlOutMessage.TEXT()
                .fromUser(wxMpXmlMessage.getToUser())
                .toUser(wxMpXmlMessage.getFromUser())
                .content(content).build();
    }

    @Override
    public String execute(WxMpXmlMessage wxMpXmlMessage,WxMpService wxMpService) {
        Gson gson = new Gson();
        String content = "";
        UserToken userToken = iUserTokenService.findByOpenId(wxMpXmlMessage.getFromUser(), MpTypeEnum.OFFACCOUNTS.getType());
        String scanInfo = wxMpXmlMessage.getScanCodeInfo().getScanResult();
        if (scanInfo.startsWith("JOIN_GROUP")) {
            int wh = scanInfo.indexOf("?fromUser=");
            String oid = "";
            String fromUser = "";
            if (wh != -1) {
                oid = scanInfo.substring(10, wh);
                fromUser = scanInfo.substring(wh + 10);
            } else {
                oid = scanInfo.substring(10);
            }
            InviteRecord inviteRecord = new InviteRecord();
            inviteRecord.setFromMem(fromUser);
            inviteRecord.setToMem(userToken.getUserId());
            inviteRecord.setToUser(wxMpXmlMessage.getFromUser());
            inviteRecord.setOid(oid);
            Map result = iGroupService.joinGroup(oid, userToken.getAccessToken());
            if ("00".equals(result.get("code"))) {
                content = "加入成功，请确认";
            } else if ("03".equals(result.get("code"))) {
                content = "你已经存在该组织，请确认";
            } else {
                content = (String) result.get("msg");
            }
            inviteRecord.setReply(gson.toJson(result));
            recordService.addInvite(inviteRecord);
        } else {
            content = scanInfo;
        }
        return content;
    }
}